package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController//返回的格式都是符合json格式的字符串
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")//swagger
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录界面
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")//swagger
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {//@RequestBody注解默认：required() default true;
        log.info("员工登录：{}", employeeLoginDTO);
//      验证账号密码是否登陆成功
        Employee employee = employeeService.login(employeeLoginDTO);
//eyJhbGciOiJIUzI1NiJ9.eyJlbXBJZCI6MSwiZXhwIjoxNzU1ODM3NTU5fQ.6bZJnj_QfLnyOaYifYCjv37st2r12I3ciW9iSsgWa9U
        Map<String, Object> claims = new HashMap<>();
        //登录成功后，生成jwt令牌  "empId"=1（员工ID）
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),//jwt秘钥 默认为itcast
                jwtProperties.getAdminTtl(),//设置jwt过期时间 默认两小时
                claims);
//封装数据，相应前端页面,使用builder构建对象，前提是要有注解@Builder
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName()) //管理员
                .token(token) //String类型 相当于xx.xx.xx
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")//swagger
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    @ApiOperation("添加员工")//swagger
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        System.out.println("当前线程的id:" + Thread.currentThread().getId());

        log.info("新增员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }
    @ApiOperation("员工分页查询")//swagger
    @GetMapping("/page")
    //这里不用添加注释@RequestBody,返回的不是json格式，而是携带在地址栏后的param，springmvc会自动封装成对象
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询，传来的参数为:{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status,Long id){//status名字一样,省略value
        log.info("启用禁用员工账号:{}{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据员工ID查找信息")
    public Result<Employee> getById(@PathVariable Long id){//todo 为什么用long数据格式来接收
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping//上面有@GetMapping("/{id}")  @PostMapping 不会冲突
    @ApiOperation("修改员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改过后员工的信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }






}
